//
//  ViewController.m
//  quanmei
//
//  Created by apple on 14/12/23.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "BaseViewController.h"
#import "UIColor+Hzl.h"
#import "CommonMacro.h"
#import "UIView+Hzl.h"

@interface BaseViewController ()

/** 顶部导航条 */
@property (nonatomic, strong) UILabel *titleLabel;

/** 导航条左按钮 */
@property (nonatomic, strong) UIButton *leftButton;

/** 导航条右按钮 */
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation BaseViewController

#pragma mark - life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置状态栏字体颜色
    // [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    // 使UIScrollView类里的控件不会下移64像素
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 设置背景颜色
    self.view.backgroundColor = [UIColor colorWithHexString:@"#EBECF0" alpha:1];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    //self.navigationController.navigationBarHidden = YES;
    
    self.tabarHiend = YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - official delegate

#pragma mark - custom delegate

#pragma mark - event response

- (void)backAction:(UIButton*)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - public Methods

- (void)setBackButton
{
    [self.leftButton setImage:[UIImage imageNamed:@"icon_return_8x14_.png"] forState:UIControlStateNormal];
    [self.leftButton setImage:[UIImage imageNamed:@"icon_return_8x14_.png"] forState:UIControlStateHighlighted];
    [self.leftButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (UIButton*)setLeftButtonWithTitle:(NSString*)title action:(SEL)action
{
    [self.leftButton setTitle:title forState:UIControlStateNormal];
    [self.leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    return self.leftButton;
}
- (UIButton*)setLeftButtonWithImageName:(NSString*)imageName action:(SEL)action
{
    [self.leftButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    return self.leftButton;
}
- (UIButton*)setRightButtonWithTitle:(NSString*)title action:(SEL)action
{
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    CGSize size = CGSizeMake(MAXFLOAT,self.rightButton.height);
    CGFloat lableW = [title sizeWithFont:self.rightButton.titleLabel.font constrainedToSize:size].width + 30;
    
    self.rightButton.x = hScreenWidth - lableW;
    self.rightButton.width = lableW;
    return self.rightButton;
}
- (UIButton*)setRightButtonWithImageName:(NSString*)imageName action:(SEL)action
{
    [self.rightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    return self.rightButton;
}

#pragma mark - private Methods

#pragma mark - getters and setters

-(void)setTitle:(NSString *)title
{
    [super setTitle:title];
    
    self.titleLabel.text = title;
}
- (void)setNavColor:(UIColor *)navColor
{
    self.navigationBar.backgroundColor = navColor;
}
- (void)setNavImage:(UIImage *)navImage
{
    self.navigationBar.image = navImage;
}
- (void)setNavHidden:(BOOL)navHidden
{
    _navHidden = navHidden;
    
    self.navigationBar.hidden = navHidden;
}
- (void)setTabarHiend:(BOOL)tabarHiend
{

}

- (UIImageView*)navigationBar
{
    if (!_navigationBar)
    {
        _navigationBar = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, hScreenWidth, hTopHeight)];
        _navigationBar.userInteractionEnabled = YES;
        _navigationBar.clipsToBounds=YES;
        _navigationBar.backgroundColor = [UIColor colorWithHexString:@"ffffff" alpha:1];
        [self.view addSubview:self.navigationBar];
    }
    
    return _navigationBar;
}
- (UILabel*)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, hStatusBarHeight, hScreenWidth - 120, hNavBarHeight)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = FONT_SIZE(18);
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.navigationBar addSubview:_titleLabel];
    }
    
    return _titleLabel;
}
- (UIButton*)leftButton
{
    if (!_leftButton)
    {
        _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(3, hStatusBarHeight, hNavBarHeight, hNavBarHeight)];
        _leftButton.backgroundColor = [UIColor clearColor];
        _leftButton.showsTouchWhenHighlighted = YES;
        [self.navigationBar addSubview:_leftButton];
    }
    
    return _leftButton;
}
- (UIButton*)rightButton
{
    if (!_rightButton)
    {
        _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(hScreenWidth - 70, 20, 70, hNavBarHeight)];
        _rightButton.backgroundColor = [UIColor clearColor];
        _rightButton.showsTouchWhenHighlighted = YES;
        _rightButton.titleLabel.font = FONT_SIZE(13);
        [self.navigationBar addSubview:_rightButton];
    }
    
    return _rightButton;
}

@end
